function Performance() {
    this.data = {};
    this.timer = 0;
    this.check();
}
var PerformancePrototype = Performance.prototype;
PerformancePrototype.name = "performance";
PerformancePrototype.check = function() {
    if (this.timer) {
        clearTimeout(this.timer);
        this.timer = 0;
    }
    this.timer = setTimeout(function(){
        var performance = window.performance || window.webkitPerformance;
        if (!performance || !performance.timing) {
            this.check();
        }else if (performance && performance.timing) {
            if (!performance.timing.loadEventEnd) {
                this.check();
            } else {
                this.get();
                // 加载完成后自动发送统计
                clientCollect.send("event", null, "performance");
            }
        }
    }.bind(this), 300);
};
PerformancePrototype.get = function(){
    var performance = window.performance || window.webkitPerformance;
    var data = {};
    var timing = performance && performance.timing;
    var navigation = performance && performance.navigation;
    var redirectCount = navigation && navigation.redirectCount;

    if (performance && timing) {
        // 开始渲染的时间
        var firstPaint = 0;
        if (window.chrome && window.chrome.loadTimes) {
            firstPaint = window.chrome.loadTimes().firstPaintTime * 1000;
            firstPaint = firstPaint - timing.navigationStart;
        }
        // dns 查询时间
        data.dns = Math.ceil(timing.domainLookupEnd - timing.domainLookupStart);
        // TCP 握手时间
        data.tcp = Math.ceil(timing.connectEnd - timing.connectStart);
        // 网络加载时间
        data.request =  Math.ceil(timing.responseEnd - timing.requestStart);
        // 接收到第一个字节时间(T0)
        data.connect = Math.ceil(timing.responseStart - timing.domainLookupStart);
        // 渲染处理
        data.processing = Math.ceil((timing.domComplete || timing.domLoading) - timing.domLoading);
        // 页面结构加载时间(T2)
        data.ready = Math.ceil(timing.domContentLoadedEventEnd - timing.navigationStart);
        // 加载时间(T3)
        data.load = Math.ceil(timing.loadEventEnd - timing.navigationStart);
        // 白屏时间
        data.white = Math.ceil(timing.responseStart - timing.navigationStart);
        // 开始渲染时间(T1)
        data.first_paint = firstPaint;
        if (!data.first_paint) {
            data.first_paint = timing.domLoading >= timing.navigationStart ? timing.domLoading - timing.navigationStart : 0;
        }
        data.first_paint = Math.ceil(data.first_paint);
        // 可交互时间
        data.active = Math.ceil(timing.domInteractive - timing.navigationStart);

    }
    this.data = data;
    var url = window.location.href;
    if (url.indexOf("#") === -1) {
        url = window.location.pathname;
    } else {
        url = url.split("#")[1];
        url = url && url.split("?")[0] || "/index";
    }
    this.data.mod_path = url;
    return this.data;
};
PerformancePrototype.report = function(){
    return extend(
        {
            "log_type": "event"
            // 事件分类，性能统计
            ,"category": "performance"
            // 分类行为，网页统计
            ,"action": "timing"
        }
        ,this.data
    )
};
clientCollect.add(Performance);
